package com.hxgz.steel.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.hxgz.steel.common.ServerResponse;
import com.hxgz.steel.dao.*;
import com.hxgz.steel.domain.AdManager;
import com.hxgz.steel.domain.NavigationManager;
import com.hxgz.steel.domain.Page;
import com.hxgz.steel.dto.*;
import com.hxgz.steel.service.HomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("homeService")
public class HomeServiceImpl implements HomeService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    private MerchantMapper merchantMapper;

    @Autowired
    private AdManagerMapper adManagerMapper;
    @Autowired
    private NavigationManagerMapper navigationManagerMapper;
    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public ServerResponse homeTotal() {
        HashMap<Object, Object> map = Maps.newHashMap();
        /*当前用户总量*/
        Integer userNumber = userMapper.getAllUserNumber();
        /*本周新增加用户*/
        Integer userNumberWeek = userMapper.getUserByWeek();
        /*当前商家总量*/
        Integer merchantNumber = merchantMapper.getAllMerchantNum();
        /*本月新增加商家*/
        Integer merchantNumberMon = merchantMapper.getMerchantNumByMon();
        /*总订单数*/
        Integer orderNum = orderMapper.getAllOrderNumByNotCancelledAndNotWaitingPaymentAndClosed();
        /*今日成交订单*/
        Integer orderNumByDay = orderMapper.getDealOrderByDayByNotCancelledAndNotWaitingPaymentAndClosed();
        /*总营收*/
        BigDecimal allMoney = orderMapper.getAllMoneyByDoneAndDoneDelete();
        /*本月新增营收*/
        BigDecimal moneyByMon = orderMapper.getMoneyByDoneAndDoneDelete();
        /*月度报表*/
        MonthReport monthReport = new MonthReport();
        List list = orderMapper.getCountByMonthTime();
        ArrayList monthList = new ArrayList();
        monthList.add(list.get(0));
        monthList.add(list.get(1));
        monthList.add(list.get(2));
        monthList.add(list.get(3));
        monthList.add(list.get(4));
        monthList.add(list.get(5));
        monthList.add(list.get(6));
        monthList.add(list.get(7));
        monthList.add(list.get(8));
        monthList.add(list.get(9));
        monthList.add(list.get(10));
        monthList.add(list.get(11));
        map.put("userNumber", userNumber);
        map.put("userNumberWeek", userNumberWeek);
        map.put("merchantNumber", merchantNumber);
        map.put("merchantNumberMon", merchantNumberMon);
        map.put("orderNum", orderNum);
        map.put("orderNumByDay", orderNumByDay);
        map.put("allMoney", allMoney);
        map.put("moneyByMon", moneyByMon);
        map.put("monthReport", monthList);
        return ServerResponse.createBySuccess(map);
    }

    @Override
    public ServerResponse revenueStatistics(Page page, Integer timeState, String startTime,
                                            String endTime, String orderCode, String merchantName) {
        Map<String, Object> map = new HashMap<>();
        map.put("orderCode", orderCode);
        map.put("merchantName", merchantName);
        map.put("startDate", startTime);
        map.put("endDate", endTime);
        map.put("timeState", timeState);
        PageHelper.startPage(page.getCurrPageNo(), page.getPageSize());
        List<CountDto> countDtos = orderMapper.getList(map);
        PageInfo pageInfo = new PageInfo(countDtos);
        return ServerResponse.createBySuccess(pageInfo);
    }

    public ServerResponse banner() {
        List<AdManager> adList = adManagerMapper.getAdList(0);
        return ServerResponse.createBySuccess(adList);
    }

    public ServerResponse getBannerList() {
        List<AdManager> adList = adManagerMapper.getBannerList(1);
        return ServerResponse.createBySuccess(adList);
    }

    public ServerResponse navigationList() {
        List<NavigationDto> natvigationList = navigationManagerMapper.getFirstName(0l);
//    for (int i = 0;i<natvigationList.size();i++){
//      natvigationList.get(i).setIsCurr("true");
//    }
        for (int i = 0; i < natvigationList.size(); i++) {
            List<NavigationDto> naCList = navigationManagerMapper.getFirstName(natvigationList.get(i).getId());
            natvigationList.get(i).setNavigationDtoList(naCList);
//      for(int j = 0;j<natvigationList.get(i).getNavigationDtoList().size();j++){
//        natvigationList.get(i).getNavigationDtoList().get(j).getIsCurr().equals("");
//      }
        }
//    natvigationList.add("",isCorr);
        return ServerResponse.createBySuccess(natvigationList);
    }

    public ServerResponse navigationInfo(Long id) {
        NavigationManager navigationManager = navigationManagerMapper.selectByPrimaryKey(id);
        return ServerResponse.createBySuccess(navigationManager);
    }

    public ServerResponse primeType(Integer pageSize) {

        List<CategoryDto> primeName = categoryMapper.getPrimeName(0);
        for (int i = 0; i < primeName.size(); i++) {
            List<CategoryDto> primeInfo = categoryMapper
                    .getPrimeType(primeName.get(i).getId(), pageSize);
            primeName.get(i).setCategoryDtoList(primeInfo);
        }
        return ServerResponse.createBySuccess(primeName);
    }

    @Override
    @Transactional
    public ServerResponse childType(Integer id, Integer pageSize) {
        List<CategoryDto> primeName = Lists.newArrayList();
        if (id != null) {
            //类别信息
            primeName = categoryMapper.getPrimeName(id);
            for (int i = 0; i < primeName.size(); i++) {
                List<CategoryDto> getChildName = categoryMapper
                        .getChildType(primeName.get(i).getId(), pageSize);
                primeName.get(i).setCategoryDtoList(getChildName);
            }
        } else {
            //新增没有id的情况
            CategoryDto categoryDto = new CategoryDto();
            categoryDto.setName("品名");
            primeName.add(categoryDto);
            CategoryDto categoryDto1 = new CategoryDto();
            categoryDto1.setName("商品名称");
            primeName.add(categoryDto1);
            CategoryDto categoryDto2 = new CategoryDto();
            categoryDto2.setName("材质");
            primeName.add(categoryDto2);
            CategoryDto categoryDto3 = new CategoryDto();
            categoryDto3.setName("规格");
            primeName.add(categoryDto3);

            for (int i = 0; i < primeName.size(); i++) {
                List<CategoryDto> getChildName = categoryMapper.getTypeByType(primeName.get(i).getName(), pageSize);
                primeName.get(i).setCategoryDtoList(getChildName);
            }

        }

        return ServerResponse.createBySuccess(primeName);
    }

    @Override
    public ServerResponse getMerchantByKind(Integer id, Integer pageSize) {
        List<HotStore> merchantByKind = merchantMapper.getMerchantByKind(id, pageSize);
    /*String obj = JsonUtil.obj2String(merchantByKind);
    RedisPoolUtil.setEx("getMerchantByKind", obj, Const.RedisCacheExtime.REDIS_DATA_EXTIME);*/
        for (HotStore m : merchantByKind) {
            String[] split = m.getBusinessScope().split("-");
            StringBuilder builder = new StringBuilder();
            for (String str : split) {
                switch (Integer.valueOf(str)) {
                    case 1:
                        builder.append("风类 ");
                        break;
                    case 2:
                        builder.append("水类 ");
                        break;
                    case 3:
                        builder.append("电类 ");
                        break;
                    case 4:
                        builder.append("消防类 ");
                        break;
                }
            }
            m.setBusinessScope("" + builder);
        }
        return ServerResponse.createBySuccess(merchantByKind);
    }


}
